package org.me.services;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.me.bean.Announcement;
import org.me.bean.CourseSchedule;
import org.me.bean.InstructorData;
import org.me.bean.Instructor;
import org.me.bean.InstructorData;
import org.me.bean.Student;
import org.me.bean.StudentData;

import android.util.Log;

public class WSlib {
	public WSlib() {
	}

	private static String SOAP_ACTION = "";

	private static String OPERATION_NAME = "";

	private static final String WSDL_TARGET_NAMESPACE = "http://service.me.org/";

	public static String SOAP_ADDRESS = "http://" + "192.168.56.1" + ":"
			+ "8080" + "/WebApp/AnnouncementService?WSDL";

	public static final String Log_Tag = "WSlib";

	public String getOtherStudent(String userId, String password) {
		String results = "";
		OPERATION_NAME = "getStudentData";
		SOAP_ACTION = "http://service.me.org/AnnouncementService/"
				+ OPERATION_NAME;

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);//

		request.addProperty("studentId", userId);
		request.addProperty("password", password);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = false;

		// envelope.addMapping(WSDL_TARGET_NAMESPACE, "AuThen",new
		// AuThen().getClass());

		envelope.setOutputSoapObject(request);

		int Time_Out = 10000;
		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

		try {
			httpTransport.call(SOAP_ACTION, envelope);
			Object response = envelope.getResponse();
			results = response.toString();
			// authen = new AuThen((SoapObject) response);

		} catch (Exception exception) {
			Log.e(Log_Tag, exception.toString());
			results = exception.toString();
		}
		return results;
	}

	public StudentData getStudentData(String studentId, String password) {
		OPERATION_NAME = "getStudentData";
		SOAP_ACTION = "http://service.me.org/AnnouncementService/"
				+ OPERATION_NAME;

		List<CourseSchedule> courseScheduleList = new ArrayList<CourseSchedule>();
		List<Announcement> annList = new ArrayList<Announcement>();
		
		StudentData stuData = new StudentData();

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);//

		request.addProperty("studentId", studentId);
		request.addProperty("password", password);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = false;
		envelope.addMapping(WSDL_TARGET_NAMESPACE, "StudentData",
				new InstructorData().getClass());
		envelope.setOutputSoapObject(request);

		int Time_Out = 10000;
		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS,
				Time_Out);

		try {

			httpTransport.call(SOAP_ACTION, envelope);

			SoapObject obj = (SoapObject) envelope.getResponse();
			if(obj == null){
				
				return null;
				
			}else{
				
				for (int i = 0; i < obj.getPropertyCount(); i++) {
					PropertyInfo info = new PropertyInfo();
					obj.getPropertyInfo(i, info);
					
					if(info.getName().equalsIgnoreCase("courseSchedule")){ // get Course Schedule list
						//String[] courseSchedule = new String[]{};
						SoapObject objCourseSchedule = (SoapObject) info.getValue();
						CourseSchedule cSchedule = new CourseSchedule(objCourseSchedule);						
												courseScheduleList.add(cSchedule);
												
					}else if(info.getName().equalsIgnoreCase("student")){
						SoapObject objStudent = (SoapObject) info.getValue();
						Student student = new Student(objStudent);
						stuData.setStudent(student);					
					}
				}
				stuData.setAnnList(annList);
				stuData.setCourseList(courseScheduleList);
				//stuData.setAnnList(annList);
			}
			
		} catch (Exception exception) {
			Log.e(Log_Tag, exception.toString());
			return null;
		}

		return stuData;
	}
	
	public InstructorData getInstructorData(String instructorId, String password){
		OPERATION_NAME = "getInstructorData";
		SOAP_ACTION = "http://service.me.org/AnnouncementService/"
				+ OPERATION_NAME;

		List<CourseSchedule> courseScheduleList = new ArrayList<CourseSchedule>();
		List<Announcement> annList = new ArrayList<Announcement>();
		
		InstructorData instData = new InstructorData();

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);//

		request.addProperty("instructorId", instructorId);
		request.addProperty("password", password);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = false;
		envelope.addMapping(WSDL_TARGET_NAMESPACE, "InstructorData",
				new InstructorData().getClass());
		envelope.setOutputSoapObject(request);

		int Time_Out = 10000;
		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS,
				Time_Out);

		try {

			httpTransport.call(SOAP_ACTION, envelope);

			SoapObject obj = (SoapObject) envelope.getResponse();
			if(obj == null){
				
				return null;
				
			}else{
				
				for (int i = 0; i < obj.getPropertyCount(); i++) {
					PropertyInfo info = new PropertyInfo();
					obj.getPropertyInfo(i, info);
					
					if(info.getName().equalsIgnoreCase("courseSchedule")){ // get Course Schedule list						
						SoapObject objCourseSchedule = (SoapObject) info.getValue();
						CourseSchedule cSchedule = new CourseSchedule(objCourseSchedule);
						courseScheduleList.add(cSchedule);
												
					}else if(info.getName().equalsIgnoreCase("instructor")){
						SoapObject objInst = (SoapObject)info.getValue();
						Instructor instructor = new Instructor(objInst);
						instData.setInstructor(instructor);
					}
				}
				
									
				//instData.setAnnList(annList);
				instData.setTeachingCourseList(courseScheduleList);
				//stuData.setAnnList(annList);
			}
			
		} catch (Exception exception) {
			Log.e(Log_Tag, exception.toString());
			return null;
		}

		return instData;

	}

	public String postAnnouncement(
			String title, 
			String content, 
			String instructorId, 
			String courseScheduleId){
		String results = "";
		
		OPERATION_NAME = "postAnnouncement";
		SOAP_ACTION = "http://service.me.org/AnnouncementService/"
				+ OPERATION_NAME;

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);//

		request.addProperty("title", title);
		request.addProperty("content", content);
		request.addProperty("postedBy", instructorId);
		request.addProperty("courseScheduleId", courseScheduleId);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = false;

		envelope.setOutputSoapObject(request);

		int Time_Out = 10000;
		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

		try {
			httpTransport.call(SOAP_ACTION, envelope);
			Object response = envelope.getResponse();
			results = response.toString();
			// authen = new AuThen((SoapObject) response);

		} catch (Exception exception) {
			Log.e(Log_Tag, exception.toString());
			results = exception.toString();
		}
		return results;
				
	}
	
	public List<Announcement> refreshAnnouncementList (String courseScheduleId){
		
		List<Announcement> annList = new ArrayList<Announcement>();
		
		OPERATION_NAME = "getAnnouncementList";
		SOAP_ACTION = "http://service.me.org/AnnouncementService/"
				+ OPERATION_NAME;

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);//

		request.addProperty("CourseScheduleId", courseScheduleId);
		

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		
		envelope.dotNet = false;
		envelope.addMapping(WSDL_TARGET_NAMESPACE, "Announcement",new Announcement().getClass());
		envelope.setOutputSoapObject(request);

		int Time_Out = 10000;
		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

		try {
			httpTransport.call(SOAP_ACTION, envelope);
			KvmSerializable ks = (KvmSerializable)envelope.bodyIn;
			
			if(ks != null){
				for (int i = 0; i < ks.getPropertyCount(); i++) {
					SoapObject obj = (SoapObject) ks.getProperty(i);
					Announcement ann = new Announcement(obj, courseScheduleId);
					annList.add(ann);
				}
			}
		} catch (Exception exception) {
			Log.e(Log_Tag, exception.toString());
		}
		return annList;
		
	}
	
	public String deleleAnnouncement(int announcementId){
		String result = "";
		
		OPERATION_NAME = "deleteAnnouncement";
		SOAP_ACTION = "http://service.me.org/AnnouncementService/"
				+ OPERATION_NAME;

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
				OPERATION_NAME);//

		request.addProperty("announcementId", announcementId);
		
		

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = false;

		// envelope.addMapping(WSDL_TARGET_NAMESPACE, "AuThen",new
		// AuThen().getClass());

		envelope.setOutputSoapObject(request);

		int Time_Out = 10000;
		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

		try {
			httpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response =  (SoapPrimitive) envelope.getResponse();
			result = response.toString();
			// authen = new AuThen((SoapObject) response);

		} catch (Exception exception) {
			Log.e(Log_Tag, exception.toString());
			
		}
		
		return result;
	}
}
